<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>视图说明 | 课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C04-View/ViewSet.html" />
    
    
    <link rel="prev" href="../C04-View/View.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.3"
        data-chapter-title="视图说明"
        data-filepath="C04-View/ViewIntroduction.md"
        data-basepath=".."
        data-revision="Thu May 10 2018 07:57:11 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-IntroduceToDRF/index.html">
            
                
                    <a href="../C01-IntroduceToDRF/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入DjangoRESTframework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
            
                
                    <a href="../C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-IntroduceToDRF/IntroduceToREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/IntroduceToREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-IntroduceToDRF/HowToDesignREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/HowToDesignREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        RESTful设计方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
            
                
                    <a href="../C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用Django开发REST接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
            
                
                    <a href="../C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-IntroduceToDRF/AboutDRF.html">
            
                
                    <a href="../C01-IntroduceToDRF/AboutDRF.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DjangoRESTframework简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-DRFProject/index.html">
            
                
                    <a href="../C02-DRFProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        DRF工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-DRFProject/InstallAndConfig.html">
            
                
                    <a href="../C02-DRFProject/InstallAndConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-DRFProject/TheFirstDRFProgram.html">
            
                
                    <a href="../C02-DRFProject/TheFirstDRFProgram.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        见识DRF的魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Serializer/index.html">
            
                
                    <a href="../C03-Serializer/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Serializer序列化器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Serializer/Declaring.html">
            
                
                    <a href="../C03-Serializer/Declaring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        定义Serializer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Serializer/Serializing.html">
            
                
                    <a href="../C03-Serializer/Serializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Serializer/Deserializing.html">
            
                
                    <a href="../C03-Serializer/Deserializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        反序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Serializer/ModelSerializer.html">
            
                
                    <a href="../C03-Serializer/ModelSerializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-View/index.html">
            
                
                    <a href="../C04-View/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-View/RequestAndResponse.html">
            
                
                    <a href="../C04-View/RequestAndResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Request与Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-View/View.html">
            
                
                    <a href="../C04-View/View.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        视图概览
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.3" data-path="C04-View/ViewIntroduction.html">
            
                
                    <a href="../C04-View/ViewIntroduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        视图说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-View/ViewSet.html">
            
                
                    <a href="../C04-View/ViewSet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        视图集ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-View/Routers.html">
            
                
                    <a href="../C04-View/Routers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Components/index.html">
            
                
                    <a href="../C05-Components/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        其他功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Components/Authentication.html">
            
                
                    <a href="../C05-Components/Authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        认证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Components/Permissions.html">
            
                
                    <a href="../C05-Components/Permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        权限
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Components/Throttling.html">
            
                
                    <a href="../C05-Components/Throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        限流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Components/Filtering.html">
            
                
                    <a href="../C05-Components/Filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        过滤
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="C05-Components/Pagination.html">
            
                
                    <a href="../C05-Components/Pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="C05-Components/Versioning.html">
            
                
                    <a href="../C05-Components/Versioning.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        版本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="C05-Components/Exceptions.html">
            
                
                    <a href="../C05-Components/Exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="C05-Components/Documents.html">
            
                
                    <a href="../C05-Components/Documents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自动生成接口文档
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x89C6;&#x56FE;&#x8BF4;&#x660E;">&#x89C6;&#x56FE;&#x8BF4;&#x660E;</h1>
<h2 id="1--&#x4E24;&#x4E2A;&#x57FA;&#x7C7B;">1.  &#x4E24;&#x4E2A;&#x57FA;&#x7C7B;</h2>
<h3 id="1&#xFF09;apiview">1&#xFF09;APIView</h3>
<p><code>rest_framework.views.APIView</code></p>
<p><code>APIView</code>&#x662F;REST framework&#x63D0;&#x4F9B;&#x7684;&#x6240;&#x6709;&#x89C6;&#x56FE;&#x7684;&#x57FA;&#x7C7B;&#xFF0C;&#x7EE7;&#x627F;&#x81EA;Django&#x7684;<code>View</code>&#x7236;&#x7C7B;&#x3002;</p>
<p><code>APIView</code>&#x4E0E;<code>View</code>&#x7684;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#x5728;&#x4E8E;&#xFF1A;</p>
<ul>
<li>&#x4F20;&#x5165;&#x5230;&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x662F;REST framework&#x7684;<code>Request</code>&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;Django&#x7684;<code>HttpRequeset</code>&#x5BF9;&#x8C61;&#xFF1B;</li>
<li>&#x89C6;&#x56FE;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;REST framework&#x7684;<code>Response</code>&#x5BF9;&#x8C61;&#xFF0C;&#x89C6;&#x56FE;&#x4F1A;&#x4E3A;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x8BBE;&#x7F6E;&#xFF08;render&#xFF09;&#x7B26;&#x5408;&#x524D;&#x7AEF;&#x8981;&#x6C42;&#x7684;&#x683C;&#x5F0F;&#xFF1B;</li>
<li>&#x4EFB;&#x4F55;<code>APIException</code>&#x5F02;&#x5E38;&#x90FD;&#x4F1A;&#x88AB;&#x6355;&#x83B7;&#x5230;&#xFF0C;&#x5E76;&#x4E14;&#x5904;&#x7406;&#x6210;&#x5408;&#x9002;&#x7684;&#x54CD;&#x5E94;&#x4FE1;&#x606F;&#xFF1B;</li>
<li>&#x5728;&#x8FDB;&#x884C;dispatch()&#x5206;&#x53D1;&#x524D;&#xFF0C;&#x4F1A;&#x5BF9;&#x8BF7;&#x6C42;&#x8FDB;&#x884C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x3001;&#x6743;&#x9650;&#x68C0;&#x67E5;&#x3001;&#x6D41;&#x91CF;&#x63A7;&#x5236;&#x3002;</li>
</ul>
<h5 id="&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;">&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;</h5>
<ul>
<li><strong>authentication_classes</strong>   &#x5217;&#x8868;&#x6216;&#x5143;&#x7956;&#xFF0C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x7C7B;</li>
<li><strong>permissoin_classes</strong>   &#x5217;&#x8868;&#x6216;&#x5143;&#x7956;&#xFF0C;&#x6743;&#x9650;&#x68C0;&#x67E5;&#x7C7B;</li>
<li><strong>throttle_classes</strong>  &#x5217;&#x8868;&#x6216;&#x5143;&#x7956;&#xFF0C;&#x6D41;&#x91CF;&#x63A7;&#x5236;&#x7C7B;</li>
</ul>
<p>&#x5728;<code>APIView</code>&#x4E2D;&#x4ECD;&#x4EE5;&#x5E38;&#x89C4;&#x7684;&#x7C7B;&#x89C6;&#x56FE;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#x6765;&#x5B9E;&#x73B0;get() &#x3001;post() &#x6216;&#x8005;&#x5176;&#x4ED6;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework.views <span class="hljs-keyword">import</span> APIView
<span class="hljs-keyword">from</span> rest_framework.response <span class="hljs-keyword">import</span> Response

<span class="hljs-comment"># url(r&apos;^books/$&apos;, views.BookListView.as_view()),</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookListView</span><span class="hljs-params">(APIView)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        books = BookInfo.objects.all()
        serializer = BookInfoSerializer(books, many=<span class="hljs-keyword">True</span>)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<h3 id="2&#xFF09;genericapiview">2&#xFF09;GenericAPIView</h3>
<p><code>rest_framework.generics.GenericAPIView</code></p>
<p>&#x7EE7;&#x627F;&#x81EA;<code>APIVIew</code>&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x5BF9;&#x4E8E;&#x5217;&#x8868;&#x89C6;&#x56FE;&#x548C;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x53EF;&#x80FD;&#x7528;&#x5230;&#x7684;&#x901A;&#x7528;&#x652F;&#x6301;&#x65B9;&#x6CD5;&#x3002;&#x901A;&#x5E38;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x53EF;&#x642D;&#x914D;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;Mixin&#x6269;&#x5C55;&#x7C7B;&#x3002;</p>
<h5 id="&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;">&#x652F;&#x6301;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#xFF1A;</h5>
<ul>
<li>&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;<ul>
<li><strong>queryset</strong>  &#x5217;&#x8868;&#x89C6;&#x56FE;&#x7684;&#x67E5;&#x8BE2;&#x96C6;</li>
<li><strong>serializer_class</strong>  &#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;</li>
</ul>
</li>
<li>&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;<ul>
<li><strong>pagination_class</strong>  &#x5206;&#x9875;&#x63A7;&#x5236;&#x7C7B;</li>
<li><strong>filter_backends</strong>  &#x8FC7;&#x6EE4;&#x63A7;&#x5236;&#x540E;&#x7AEF;</li>
</ul>
</li>
<li>&#x8BE6;&#x60C5;&#x9875;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;<ul>
<li><strong>lookup_field</strong>  &#x67E5;&#x8BE2;&#x5355;&#x4E00;&#x6570;&#x636E;&#x5E93;&#x5BF9;&#x8C61;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x6761;&#x4EF6;&#x5B57;&#x6BB5;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&apos;<code>pk</code>&apos;</li>
<li><strong>lookup_url_kwarg</strong>  &#x67E5;&#x8BE2;&#x5355;&#x4E00;&#x6570;&#x636E;&#x65F6;URL&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x5173;&#x952E;&#x5B57;&#x540D;&#x79F0;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E0E;<strong>look_field</strong>&#x76F8;&#x540C;    </li>
</ul>
</li>
</ul>
<h5 id="&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;">&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</h5>
<ul>
<li><p>&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x901A;&#x7528;&#xFF1A;</p>
<ul>
<li><p><strong>get_queryset(self)</strong> </p>
<p>&#x8FD4;&#x56DE;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#x7684;&#x67E5;&#x8BE2;&#x96C6;&#xFF0C;&#x662F;&#x5217;&#x8868;&#x89C6;&#x56FE;&#x4E0E;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x57FA;&#x7840;&#xFF0C;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;<code>queryset</code>&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_queryset</span><span class="hljs-params">(self)</span>:</span>
    user = self.request.user
    <span class="hljs-keyword">return</span> user.accounts.all()
</code></pre>
</li>
<li><p><strong>get_serializer_class(self)</strong>   </p>
<p>&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7C7B;&#xFF0C;&#x9ED8;&#x8BA4;&#x8FD4;&#x56DE;<code>serializer_class</code>&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_serializer_class</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-keyword">if</span> self.request.user.is_staff:
        <span class="hljs-keyword">return</span> FullAccountSerializer
    <span class="hljs-keyword">return</span> BasicAccountSerializer
</code></pre>
</li>
<li><h5 id="getserializerself-args-kwargs">get_serializer(self, <em>args, *</em>kwargs)</h5>
<p>&#x8FD4;&#x56DE;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x88AB;&#x5176;&#x4ED6;&#x89C6;&#x56FE;&#x6216;&#x6269;&#x5C55;&#x7C7B;&#x4F7F;&#x7528;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x89C6;&#x56FE;&#x4E2D;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x63D0;&#x4F9B;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#xFF0C;REST framework&#x4F1A;&#x5411;&#x5BF9;&#x8C61;&#x7684;context&#x5C5E;&#x6027;&#x8865;&#x5145;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#xFF1A;request&#x3001;format&#x3001;view&#xFF0C;&#x8FD9;&#x4E09;&#x4E2A;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x5728;&#x5B9A;&#x4E49;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x65F6;&#x4F7F;&#x7528;&#x3002;</strong></p>
</li>
</ul>
</li>
<li><p>&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x4F7F;&#x7528;&#xFF1A;</p>
<ul>
<li><p><strong>get_object(self)</strong>
&#x8FD4;&#x56DE;&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x6240;&#x9700;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;<code>lookup_field</code>&#x53C2;&#x6570;&#x6765;&#x8FC7;&#x6EE4;queryset&#x3002;
&#x5728;&#x8BD5;&#x56FE;&#x4E2D;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x8BE6;&#x60C5;&#x4FE1;&#x606F;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#x3002;</p>
<p><strong>&#x82E5;&#x8BE6;&#x60C5;&#x8BBF;&#x95EE;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x5BF9;&#x8C61;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x4F1A;&#x8FD4;&#x56DE;404&#x3002;</strong></p>
<p><strong>&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;APIView&#x63D0;&#x4F9B;&#x7684;check_object_permissions&#x65B9;&#x6CD5;&#x68C0;&#x67E5;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x6709;&#x6743;&#x9650;&#x88AB;&#x8BBF;&#x95EE;&#x3002;</strong></p>
</li>
</ul>
</li>
</ul>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># url(r&apos;^books/(?P&lt;pk&gt;\d+)/$&apos;, views.BookDetailView.as_view()),</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookDetailView</span><span class="hljs-params">(GenericAPIView)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request, pk)</span>:</span>
        book = self.get_object()
        serializer = self.get_serializer(book)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<h2 id="2--&#x4E94;&#x4E2A;&#x6269;&#x5C55;&#x7C7B;">2.  &#x4E94;&#x4E2A;&#x6269;&#x5C55;&#x7C7B;</h2>
<h3 id="1&#xFF09;listmodelmixin">1&#xFF09;ListModelMixin</h3>
<p>&#x5217;&#x8868;&#x89C6;&#x56FE;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x63D0;&#x4F9B;<code>list(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#x5FEB;&#x901F;&#x5B9E;&#x73B0;&#x5217;&#x8868;&#x89C6;&#x56FE;&#xFF0C;&#x8FD4;&#x56DE;200&#x72B6;&#x6001;&#x7801;&#x3002;</p>
<p>&#x8BE5;Mixin&#x7684;list&#x65B9;&#x6CD5;&#x4F1A;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;&#x548C;&#x5206;&#x9875;&#x3002;</p>
<p>&#x6E90;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ListModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    List a queryset.
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        <span class="hljs-comment"># &#x8FC7;&#x6EE4;</span>
        queryset = self.filter_queryset(self.get_queryset())
        <span class="hljs-comment"># &#x5206;&#x9875;</span>
        page = self.paginate_queryset(queryset)
        <span class="hljs-keyword">if</span> page <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">None</span>:
            serializer = self.get_serializer(page, many=<span class="hljs-keyword">True</span>)
            <span class="hljs-keyword">return</span> self.get_paginated_response(serializer.data)
        <span class="hljs-comment"># &#x5E8F;&#x5217;&#x5316;</span>
        serializer = self.get_serializer(queryset, many=<span class="hljs-keyword">True</span>)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework.mixins <span class="hljs-keyword">import</span> ListModelMixin

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookListView</span><span class="hljs-params">(ListModelMixin, GenericAPIView)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-keyword">return</span> self.list(request)
</code></pre>
<h3 id="2&#xFF09;createmodelmixin">2&#xFF09;CreateModelMixin</h3>
<p>&#x521B;&#x5EFA;&#x89C6;&#x56FE;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x63D0;&#x4F9B;<code>create(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#x5FEB;&#x901F;&#x5B9E;&#x73B0;&#x521B;&#x5EFA;&#x8D44;&#x6E90;&#x7684;&#x89C6;&#x56FE;&#xFF0C;&#x6210;&#x529F;&#x8FD4;&#x56DE;201&#x72B6;&#x6001;&#x7801;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x524D;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x5931;&#x8D25;&#xFF0C;&#x8FD4;&#x56DE;400&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x6E90;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreateModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    Create a model instance.
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5E8F;&#x5217;&#x5316;&#x5668;</span>
        serializer = self.get_serializer(data=request.data)
        <span class="hljs-comment"># &#x9A8C;&#x8BC1;</span>
        serializer.is_valid(raise_exception=<span class="hljs-keyword">True</span>)
        <span class="hljs-comment"># &#x4FDD;&#x5B58;</span>
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        <span class="hljs-keyword">return</span> Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform_create</span><span class="hljs-params">(self, serializer)</span>:</span>
        serializer.save()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_success_headers</span><span class="hljs-params">(self, data)</span>:</span>
        <span class="hljs-keyword">try</span>:
            <span class="hljs-keyword">return</span> {<span class="hljs-string">&apos;Location&apos;</span>: str(data[api_settings.URL_FIELD_NAME])}
        <span class="hljs-keyword">except</span> (TypeError, KeyError):
            <span class="hljs-keyword">return</span> {}
</code></pre>
<h3 id="3&#xFF09;-retrievemodelmixin">3&#xFF09; RetrieveModelMixin</h3>
<p>&#x8BE6;&#x60C5;&#x89C6;&#x56FE;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x63D0;&#x4F9B;<code>retrieve(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x5B9E;&#x73B0;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B58;&#x5728;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;200&#xFF0C; &#x5426;&#x5219;&#x8FD4;&#x56DE;404&#x3002;</p>
<p>&#x6E90;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RetrieveModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    Retrieve a model instance.
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">retrieve</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#xFF0C;&#x4F1A;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x7684;&#x6743;&#x9650;</span>
        instance = self.get_object()
        <span class="hljs-comment"># &#x5E8F;&#x5217;&#x5316;</span>
        serializer = self.get_serializer(instance)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookDetailView</span><span class="hljs-params">(RetrieveModelMixin, GenericAPIView)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request, pk)</span>:</span>
        <span class="hljs-keyword">return</span> self.retrieve(request)
</code></pre>
<h3 id="4&#xFF09;updatemodelmixin">4&#xFF09;UpdateModelMixin</h3>
<p>&#x66F4;&#x65B0;&#x89C6;&#x56FE;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x63D0;&#x4F9B;<code>update(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x5B9E;&#x73B0;&#x66F4;&#x65B0;&#x4E00;&#x4E2A;&#x5B58;&#x5728;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x540C;&#x65F6;&#x4E5F;&#x63D0;&#x4F9B;<code>partial_update(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x5C40;&#x90E8;&#x66F4;&#x65B0;&#x3002;</p>
<p>&#x6210;&#x529F;&#x8FD4;&#x56DE;200&#xFF0C;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x6821;&#x9A8C;&#x6570;&#x636E;&#x5931;&#x8D25;&#x65F6;&#xFF0C;&#x8FD4;&#x56DE;400&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x6E90;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UpdateModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    Update a model instance.
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        partial = kwargs.pop(<span class="hljs-string">&apos;partial&apos;</span>, <span class="hljs-keyword">False</span>)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=<span class="hljs-keyword">True</span>)
        self.perform_update(serializer)

        <span class="hljs-keyword">if</span> getattr(instance, <span class="hljs-string">&apos;_prefetched_objects_cache&apos;</span>, <span class="hljs-keyword">None</span>):
            <span class="hljs-comment"># If &apos;prefetch_related&apos; has been applied to a queryset, we need to</span>
            <span class="hljs-comment"># forcibly invalidate the prefetch cache on the instance.</span>
            instance._prefetched_objects_cache = {}

        <span class="hljs-keyword">return</span> Response(serializer.data)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform_update</span><span class="hljs-params">(self, serializer)</span>:</span>
        serializer.save()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">partial_update</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        kwargs[<span class="hljs-string">&apos;partial&apos;</span>] = <span class="hljs-keyword">True</span>
        <span class="hljs-keyword">return</span> self.update(request, *args, **kwargs)
</code></pre>
<h3 id="5&#xFF09;destroymodelmixin">5&#xFF09;DestroyModelMixin</h3>
<p>&#x5220;&#x9664;&#x89C6;&#x56FE;&#x6269;&#x5C55;&#x7C7B;&#xFF0C;&#x63D0;&#x4F9B;<code>destroy(request, *args, **kwargs)</code>&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x5B9E;&#x73B0;&#x5220;&#x9664;&#x4E00;&#x4E2A;&#x5B58;&#x5728;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x6210;&#x529F;&#x8FD4;&#x56DE;204&#xFF0C;&#x4E0D;&#x5B58;&#x5728;&#x8FD4;&#x56DE;404&#x3002;</p>
<p>&#x6E90;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DestroyModelMixin</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    Destroy a model instance.
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">destroy</span><span class="hljs-params">(self, request, *args, **kwargs)</span>:</span>
        instance = self.get_object()
        self.perform_destroy(instance)
        <span class="hljs-keyword">return</span> Response(status=status.HTTP_204_NO_CONTENT)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform_destroy</span><span class="hljs-params">(self, instance)</span>:</span>
        instance.delete()
</code></pre>
<h2 id="3--&#x51E0;&#x4E2A;&#x53EF;&#x7528;&#x5B50;&#x7C7B;&#x89C6;&#x56FE;">3.  &#x51E0;&#x4E2A;&#x53EF;&#x7528;&#x5B50;&#x7C7B;&#x89C6;&#x56FE;</h2>
<h3 id="1&#xFF09;-createapiview">1&#xFF09; CreateAPIView</h3>
<p>&#x63D0;&#x4F9B; post &#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A; GenericAPIView&#x3001;CreateModelMixin</p>
<h3 id="2&#xFF09;listapiview">2&#xFF09;ListAPIView</h3>
<p>&#x63D0;&#x4F9B; get &#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A;GenericAPIView&#x3001;ListModelMixin</p>
<h3 id="3&#xFF09;retireveapiview">3&#xFF09;RetireveAPIView</h3>
<p>&#x63D0;&#x4F9B; get &#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;:  GenericAPIView&#x3001;RetrieveModelMixin</p>
<h3 id="4&#xFF09;destoryapiview">4&#xFF09;DestoryAPIView</h3>
<p>&#x63D0;&#x4F9B; delete &#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A;GenericAPIView&#x3001;DestoryModelMixin</p>
<h3 id="5&#xFF09;updateapiview">5&#xFF09;UpdateAPIView</h3>
<p>&#x63D0;&#x4F9B; put &#x548C; patch &#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A;GenericAPIView&#x3001;UpdateModelMixin</p>
<h3 id="6&#xFF09;retrieveupdateapiview">6&#xFF09;RetrieveUpdateAPIView</h3>
<p>&#x63D0;&#x4F9B; get&#x3001;put&#x3001;patch&#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A; GenericAPIView&#x3001;RetrieveModelMixin&#x3001;UpdateModelMixin</p>
<h3 id="7&#xFF09;retrieveupdatedestoryapiview">7&#xFF09;RetrieveUpdateDestoryAPIView</h3>
<p>&#x63D0;&#x4F9B; get&#x3001;put&#x3001;patch&#x3001;delete&#x65B9;&#x6CD5;</p>
<p>&#x7EE7;&#x627F;&#x81EA;&#xFF1A;GenericAPIView&#x3001;RetrieveModelMixin&#x3001;UpdateModelMixin&#x3001;DestoryModelMixin</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C04-View/View.html" class="navigation navigation-prev " aria-label="Previous page: 视图概览"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C04-View/ViewSet.html" class="navigation navigation-next " aria-label="Next page: 视图集ViewSet"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
